package nettyTest.netty4test1.fourpart;

import java.io.IOException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.util.Iterator;
import java.util.Set;

public class Reactor implements Runnable {
    Selector selector;
    ServerSocketChannel serverSocket;

    void echoServerReactor() throws IOException {
        //....省略：打开选择器、serverSocket 连接监听通道
        //注册 serverSocket 的 accept 事件
        SelectionKey sk = serverSocket.register(selector, SelectionKey.OP_ACCEPT);
        //将新连接处理器作为附件，绑定到 sk 选择键
        sk.attach(new AcceptorHandler());
    }

    public void run() {
        //选择器轮询
        try {
            while (!Thread.interrupted()) {
                selector.select();
                Set<SelectionKey> selected = selector.selectedKeys();
                Iterator it = selected.iterator();
                while (it.hasNext()) {
                    //反应器负责 dispatch 收到的事件
                    SelectionKey sk = (SelectionKey) it.next();
                    dispatch(sk);
                }
                selected.clear();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    //反应器的分发方法
    void dispatch(SelectionKey k) {
        Runnable handler = (Runnable) (k.attachment());
        //调用之前绑定到选择键的 handler 处理器对象
        if (handler != null) {
            handler.run();
        }
    }

    // 新连接处理器
    class AcceptorHandler implements Runnable {
        public void run() {
            //接受新连接
            //需要为新连接，创建一个输入输出的 handler 处理器
        }
    }
//….
}
